package com.baox.agridata.synchronous.util;

import com.baox.agridata.synchronous.result.HttpResultResponse;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.json.XML;

import java.net.URLEncoder;

@Slf4j
public class MessageUtil {

    public static String BASEURL = "http://sms.800617.com:4400/sms/SendSMS.aspx";
    public static String CONTENT = "【智慧动监】四川同步发生了错误，请抓紧查看，出问题的类是：%s,出问题的方法是：%s";
    public static String ACCOUNTNAME = "bjbxsy-1";
    public static String ACCOUNTPASSWORD = "4cef27";
    public static String sendPhone = "15235744611";


    public static Integer sendMessageOther(String className, String methodName) {
        String content = String.format(CONTENT, className, methodName);
        String smsContent = URLEncoder.encode(content);
        StringBuilder url = new StringBuilder();
        url.append(BASEURL).append("/?");
        url.append("un=").append(ACCOUNTNAME).append("&");
        url.append("pwd=").append(ACCOUNTPASSWORD).append("&");
        url.append("mobile=").append(sendPhone).append("&");
        url.append("msg=").append(smsContent);
        HttpResultResponse resultResponse = HttpUtils.get(url.toString());
        JSONObject result = XML.toJSONObject(resultResponse.getBody());
        log.info("发送短信返回结果是：{}", result.toString());
        System.out.println(result.toString());
        JSONObject rootJsonObj = result.getJSONObject("Root");
        Integer sendResult = rootJsonObj.getInt("Result");
        if (sendResult != null && sendResult.intValue() == 1) {
            return sendResult.intValue();
        }
        return 0;
    }

}
